﻿using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Media;
using AvalonDock;
using Poet.Properties;

namespace Poet.Modules
{
    class ThemeManager
    {
        private static readonly Color DefaultColor = Colors.Silver;

        /// <summary>
        /// Gets the name of the current theme.
        /// </summary>
        /// <param name="pszThemeFileName">Name of the PSZ theme file.</param>
        /// <param name="dwMaxNameChars">The dw max name chars.</param>
        /// <param name="pszColorBuff">The PSZ color buff.</param>
        /// <param name="dwMaxColorChars">The dw max color chars.</param>
        /// <param name="pszSizeBuff">The PSZ size buff.</param>
        /// <param name="cchMaxSizeChars">The CCH max size chars.</param>
        /// <returns>Returns S_OK if successful, otherwise an error code.</returns>
        [DllImport("uxtheme.dll", CharSet = CharSet.Auto)]
        public static extern int GetCurrentThemeName(
            StringBuilder pszThemeFileName,
            int dwMaxNameChars,
            StringBuilder pszColorBuff,
            int dwMaxColorChars,
            StringBuilder pszSizeBuff,
            int cchMaxSizeChars);

        /// <summary>
        /// Sets the default theme.
        /// </summary>
        public static void SetDefaultAvalonDockTheme()
        {
            if(Settings.Default.AvalonDockTheme != "Default")
            {
                SetAvalonDockTheme(Settings.Default.AvalonDockTheme);
                return;
            }

            SetAvalonDockColor(Settings.Default.AvalonDockColor);
            /*
            var themeFileName = new StringBuilder(0x200);
            GetCurrentThemeName(themeFileName, themeFileName.Capacity, null, 0, null, 0);

            if (string.IsNullOrEmpty(themeFileName.ToString()))
                ThemeFactory.ChangeTheme("classic");
            else SetAvalonDockColor(Settings.Default.AvalonDockColor);
             */
        }

        /// <summary>
        /// Enables the visual styles for WinForms controls.
        /// </summary>
        public static void EnableVisualStylesForWinFormsControls()
        {
            System.Windows.Forms.Application.EnableVisualStyles();
        }

        /// <summary>
        /// Sets the defaults.
        /// </summary>
        public static void SetDefaults()
        {
            SetAvalonDockColor(DefaultColor.ToString());
            Settings.Default.AvalonDockTheme = "Default";
            Settings.Default.Save();
            SetDefaultAvalonDockTheme();
        }

        /// <summary>
        /// Sets the avalon dock theme.
        /// </summary>
        /// <param name="name">The name.</param>
        public static void SetAvalonDockTheme(string name)
        {
            try
            {
                ThemeFactory.ChangeTheme(name);
                Settings.Default.AvalonDockTheme = name;
                Settings.Default.Save();
            }
            catch
            {
                ThemeFactory.ChangeTheme(new Uri(name, UriKind.RelativeOrAbsolute));
                Settings.Default.AvalonDockTheme = name;
                Settings.Default.Save();
            }
        }

        /// <summary>
        /// Sets the color of the avalon dock.
        /// </summary>
        /// <param name="colorName">Name of the color.</param>
        public static void SetAvalonDockColor(string colorName)
        {
            var color = ColorConverter.ConvertFromString(colorName);
            ThemeFactory.ChangeColors(color == null ? DefaultColor : (Color) color);
            Settings.Default.AvalonDockColor = colorName;
            Settings.Default.AvalonDockTheme = "Default";
            Settings.Default.Save();
        }
    }
}
